namespace WinFormsApp1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void btnSelectFile2_Click(object sender, EventArgs e)
        {
            using (OpenFileDialog openFileDialog = new OpenFileDialog())
            {
                if (openFileDialog.ShowDialog() == DialogResult.OK)
                {
                    txtFile2.Text = openFileDialog.FileName;
                }
            }
        }


        private void btnSelectFile1_Click(object sender, EventArgs e)
        {
            using (OpenFileDialog openFileDialog = new OpenFileDialog())
            {
                if (openFileDialog.ShowDialog() == DialogResult.OK)
                {
                    txtFile1.Text = openFileDialog.FileName;
                }
            }
        }

        private void btnMergeAndSave_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrWhiteSpace(txtFile1.Text) || string.IsNullOrWhiteSpace(txtFile2.Text))
            {
                MessageBox.Show("Please select both files first.");
                return;
            }

            try
            {
                string dataDir = Path.Combine(Application.StartupPath, "Data");
                if (!Directory.Exists(dataDir))
                {
                    Directory.CreateDirectory(dataDir);
                }

                string mergedFilePath = Path.Combine(dataDir, "MergedFile.txt");

                // Reading and merging the content
                string file1Content = File.ReadAllText(txtFile1.Text);
                string file2Content = File.ReadAllText(txtFile2.Text);
                string mergedContent = file1Content + Environment.NewLine + file2Content;

                // Writing to the new file
                File.WriteAllText(mergedFilePath, mergedContent);

                MessageBox.Show($"Files were merged successfully. Merged file is located at: {mergedFilePath}");
            }
            catch (Exception ex)
            {
                MessageBox.Show($"An error occurred: {ex.Message}");
            }
        }

    }
}